var express = require('express');
var router = express.Router();
const db = require('../db/db');
const promise = require("../db/promise");
const fs = require("fs");//文件系统

// 解决跨域问题
// router.use((req, res, next) => {
// 	//设置请求头
// 	res.set({
// 		'Access-Control-Allow-Credentials': true,
// 		'Access-Control-Max-Age': 1728000,
// 		'Access-Control-Allow-Origin': req.headers.origin || '*',
// 		'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
// 		'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
// 		'Content-Type': 'application/json; charset=utf-8'
// 	})
// 	req.method === 'OPTIONS' ? res.status(204).end() : next()
// })

/* GET home page. */
router.get('/', function(req, res, next) {
	// res.render('index', {
	// 	title: 'Express'
	// });
	fs.readFile('./json/test.json', (err, data) => {
	    if(err){
	      throw err;
	    }
	    res.json(data.toString('utf-8'));
	  })
});

/* 主页查看更多接口 */
router.get('/comment', (req, res, next) => {
	fs.readFile('./json/comment.json', (err, data) => {
	    if(err){
	      throw err;
	    }
	    res.json(data.toString('utf-8'));
	  })
})

/* 程序员列表接口 */
router.get('/cat',(req,res,next) =>{
	fs.readFile('./json/cat.json',(err,data) => {
		if(err){
			throw err;
		}
		res.json(data.toString('utf-8'));
	})
})

/* 服务方案表格接口 */
router.get("/service",(req,res,next)=>{
	let sql = `select * from service_plan`;
	db(sql,(err,data)=>{
		res.json(data);
	})
})


module.exports = router;
